#include "pe43702.h"

const uint16_t attenuation_table[11] = {0, 7, 15, 23, 31, 39, 47, 54, 62, 70, 78};

/************************************************************
            PE43702 驱动程序
					PE43702 <-->STM32F407ZGT6
硬件连接:  DATA		<——>PC0;     
						CLK		<——>PC1;   
						LE		<——>PC2;	  
**************************************************************/

/**************************************************************** 
函数名称: PE43702Set 
功    能: 衰减值设置 
参    数: db，范围：19~99，对应衰减0~20dB
返回值  : 无 
*****************************************************************/ 
void PE43702Set(unsigned char db)
{
  unsigned char i;
  unsigned char W_DB;
  db = db&0x7f;
  PE_CLK_0;
  HAL_Delay(1);
  PE_LE_0;
  HAL_Delay(1);
  for(i = 0; i < 8;i++)
  {
    W_DB = db&0x01;
    if(W_DB == 1)
    {
      PE_DAT_1;
    }
    else
    {
      PE_DAT_0;
    }
    HAL_Delay(1);
    PE_CLK_1;
    HAL_Delay(1);
		db=db>>1;
    PE_CLK_0;
    HAL_Delay(1);
  }
  PE_LE_1;
}

//void PE43702Set2(unsigned char db)
//{
//  unsigned char i;
//  unsigned char W_DB;
//  db = db&0x7f;
//  PE2_CLK_0;
//  PE2_LE_0;
//  for(i = 0; i < 8;i++)
//  {
//    W_DB = db&0x01;
//    if(W_DB == 1)
//    {
//      PE2_DAT_1;
//    }
//    else
//    {
//      PE2_DAT_0;
//    }
//    PE2_CLK_1;
//		db=db>>1;
//    PE2_CLK_0;
//  }
//  PE2_LE_1;
//}

void set_pe43702_attenuation_dB(unsigned char dB){
	PE43702Set(attenuation_table[dB/2]);
}
